import tools from './tools'

// socket body 数据顺序操作 send时使用
export const bodyDataController = [
  // 特殊情况单独处理user_id
  {
    name: 'user_id',
    data: null,
    send (userID) {
      // eslint-disable-next-line no-undef
      return tools.dataDealer.numToUint8(userID, BigUint64Array)
    }
  },
  // 会议-是否开启麦克风
  {
    name: 'voiced',
    data: null,
    send (voiced) {
      return tools.dataDealer.numToUint8(Number(voiced), Uint8Array)
    }
  },
  // 会议-是否开启摄像头
  {
    name: 'camera',
    data: null,
    send (camera) {
      return tools.dataDealer.numToUint8(Number(camera), Uint8Array)
    }
  },
  // 会议-加入会议方式（0-会议编号，1-会议码）
  {
    name: 'cf_join_type',
    data: null,
    send (cfJoinType) {
      return tools.dataDealer.numToUint8(cfJoinType, Uint8Array)
    }
  },
  // 消息类型（1.offer 2.answer 3.candidate)
  {
    name: 'msg_type',
    data: null,
    send (msgType) {
      return tools.dataDealer.numToUint8(msgType, Uint8Array)
    }
  },
  // 会议-会议编号/会议码
  {
    name: 'cf_id',
    data: null,
    send (cfID) {
      if (typeof cfID === 'number') {
        // eslint-disable-next-line no-undef
        return tools.dataDealer.numToUint8(cfID, BigUint64Array)
      } else {
        return tools.dataDealer.strToUint8(cfID, Uint8Array)
      }
    }
  },
  // 会议-会议密码
  {
    name: 'cf_pwd',
    data: null,
    send (cfPwd) {
      return tools.dataDealer.strToUint8(cfPwd, Uint8Array)
    }
  },
  // id_type
  {
    name: 'id_type',
    data: null,
    send (idType) {
      return tools.dataDealer.numToUint8(idType, Uint8Array)
    }
  },
  // 被邀请入会人员数组
  {
    name: 'invite_ids',
    data: null,
    send (inviteIds) {
      let uint = new Uint8Array(0)
      for (let i = 0; i < inviteIds.length; i++) {
        // eslint-disable-next-line no-undef
        const idUint = tools.dataDealer.numToUint8(inviteIds[i], BigUint64Array)
        uint = tools.dataDealer.concatenate(Uint8Array, uint, idUint)
      }
      return uint
    }
  },
  // user_id
  {
    name: 'id',
    data: null,
    send (id) {
      // eslint-disable-next-line no-undef
      return tools.dataDealer.numToUint8(id, BigUint64Array)
    }
  },
  // 事件戳 timestamp
  {
    name: 'timestamp',
    data: null,
    send (timestamp) {
      // 将本地时间转化为世界时间
      const utcTimestamp = tools.time.getUTCTime(timestamp)
      // eslint-disable-next-line no-undef
      return tools.dataDealer.numToUint8(utcTimestamp, BigUint64Array)
    }
  },
  // msg 消息文本
  {
    name: 'msg',
    data: null,
    send (msg) {
      return tools.dataDealer.strToUint8(msg, Uint8Array)
    }
  },

  {
    // 图片id
    name: 'img_id',
    data: null,
    send (imgID) {
      // eslint-disable-next-line no-undef
      return tools.dataDealer.numToUint8(imgID, BigUint64Array)
    }
  },
  {
    // 图片宽度
    name: 'img_width',
    data: null,
    send (imgWidth) {
      return tools.dataDealer.numToUint8(imgWidth, Uint16Array)
    }
  },
  {
    // 图片高度
    name: 'img_height',
    data: null,
    send (imgHeight) {
      return tools.dataDealer.numToUint8(imgHeight, Uint16Array)
    }
  },
  {
    // 文件id
    name: 'file_id',
    data: null,
    send (fileID) {
      // eslint-disable-next-line no-undef
      return tools.dataDealer.numToUint8(fileID, BigUint64Array)
    }
  },
  {
    // 发送文件大小
    name: 'file_size',
    data: null,
    send (fileSize) {
      return tools.dataDealer.numToUint8(fileSize, Uint32Array)
    }
  },
  {
    // 文件名
    name: 'file_name',
    data: null,
    send (fileName) {
      return tools.dataDealer.strToUint8(fileName, Uint8Array)
    }
  },
  // 页数
  {
    name: 'page',
    data: null,
    send (page) {
      return tools.dataDealer.numToUint8(page, Uint32Array)
    }
  },
  // 单页数量
  {
    name: 'page_size',
    data: null,
    send (pageSize) {
      return tools.dataDealer.numToUint8(pageSize, Uint16Array)
    }
  },
  // msg_id_length
  {
    name: 'msg_id_length',
    data: null,
    send (msgIDLength) {
      return tools.dataDealer.numToUint8(msgIDLength, Uint16Array)
    }
  },
  // msgID
  {
    name: 'msg_id',
    data: null,
    send (msgID) {
      return tools.dataDealer.strToUint8(msgID, Uint8Array)
    }
  },
  // 视频通话 offer answer
  {
    name: 'spe_msg',
    data: null,
    send (speMsg) {
      return tools.dataDealer.strToUint8(speMsg, Uint8Array)
    }
  },
  // 会议-P2P-消息类型 (1-音频、2-视频）
  {
    name: 'candidate_type',
    data: null,
    send (candidateType) {
      return tools.dataDealer.numToUint8(candidateType, Uint8Array)
    }
  },
  // 会议—P2P-消息内容
  {
    name: 'candidate_content',
    data: null,
    send (candidateContent) {
      return tools.dataDealer.strToUint8(candidateContent, Uint8Array)
    }
  },
  // 会议-操作类型
  {
    name: 'cf_operation',
    data: null,
    send (cfOperation) {
      return tools.dataDealer.numToUint8(Number(!cfOperation), Uint8Array)
    }
  },
  // 设备类型（1-android、2-ios、3-web）
  {
    name: 'deviceType',
    data: null,
    send (deviceType) {
      return tools.dataDealer.numToUint8(deviceType, Uint8Array)
    }
  },
  // token
  {
    name: 'token',
    data: null,
    send (token) {
      return tools.dataDealer.strToUint8(token, Uint8Array)
    }
  }
]

// 批量处理各种键值对
export const setBodyDataControllerData = (bodyData) => {
  for (let i = 0; i < bodyDataController.length; i++) {
    if (bodyData[bodyDataController[i].name] || bodyData[bodyDataController[i].name] === 0 || bodyData[bodyDataController[i].name] === false) {
      bodyDataController[i].data = bodyDataController[i].send(bodyData[bodyDataController[i].name])
    }
  }
}

// 清空bodyDataController
export const clearBodyDataController = () => {
  bodyDataController.forEach(item => {
    item.data = null
  })
}
